package global
{
	//资源管理类
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	
	import flash3.framework.Singleton;
	
	import libs.bulkloader.BulkLoader;
	import libs.bulkloader.BulkProgressEvent;
	import libs.bulkloader.lazyloaders.LazyBulkLoader;
	import libs.bulkloader.lazyloaders.LazyXMLLoader;
	import libs.bulkloader.loadingtypes.LoadingItem;

	public class Resource
	{
		public var bulkLoader:BulkLoader;
		public var lazyLoader:LazyXMLLoader;
		public var resourceXml:XML = null;
		public var basePath:String;
		public static const CONFIG:String = "assets/LoadFile.xml";

		public function Resource():void
		{
			bulkLoader = BulkLoader.getLoader("global");
//			lazyLoader = new LazyXMLLoader("assets/LoadFile.xml", "main");
		}
		static public function get i():Resource
		{
			return Singleton.getInstanceOrCreate(Resource) as Resource;
		}
		public function getMovieClip(id:String, className:String):*
		{
			var cls:Class = getClass(id, className);
			return cls ? (new cls as MovieClip) : null;
		}
		public function getBitmap(id:String):Bitmap
		{
			return bulkLoader.getBitmap(id);
		}
		public function getBitmapData(id:String):BitmapData
		{
			return bulkLoader.getBitmapData(id);
		}
		public function getClass(id:String, className:String):Class
		{
			var li:LoadingItem = null;
			li = bulkLoader.get(id) as LoadingItem;
			return li.getDefinitionByName(className) as Class;
		}
		public function loadConfig(compFunc:Function = null):void
		{
			configCompFunc = compFunc;
			bulkLoader.add(CONFIG);
			bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, onLoadConfigComplete);
			bulkLoader.start();
		}
		private function onLoadConfigComplete(evt:BulkProgressEvent):void
		{
			bulkLoader.removeEventListener(BulkProgressEvent.COMPLETE, onLoadConfigComplete);
			resourceXml = bulkLoader.getXML(CONFIG);
			basePath = resourceXml.basepath;
			configCompFunc();
			configCompFunc = null;
		}
		public function loadModule(moduleName:String, compFunc:Function = null, progFunc:Function = null):void
		{
			var xmlList:XMLList = resourceXml.module.(@name == moduleName);
			for each(var xml:XML in xmlList)
			{
				var fileList:XMLList = xml.file;
				for each(var fileXml:XML in fileList)
					bulkLoader.add(basePath + fileXml.url, {id:fileXml.id});
			}
			moduleCompFunc = compFunc;
			moduleProgFunc = progFunc;
			bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, onLoadModuleComplate);
			bulkLoader.addEventListener(BulkProgressEvent.PROGRESS, onLoadModuleProgress);
			bulkLoader.start();
			if(bulkLoader.isFinished)
				onLoadModuleComplate(null);
		}
		public function setCall(compFunc:Function, progFunc:Function):void
		{
			moduleCompFunc = compFunc;
			moduleProgFunc = progFunc;
		}
		public function onLoadModuleComplate(evt:BulkProgressEvent):void
		{
			bulkLoader.removeEventListener(BulkProgressEvent.COMPLETE, onLoadModuleComplate);
			bulkLoader.removeEventListener(BulkProgressEvent.PROGRESS, onLoadModuleProgress);
			if(moduleCompFunc != null)
				moduleCompFunc(evt);
			moduleCompFunc = null;
			moduleProgFunc = null;
		}
		public function onLoadModuleProgress(evt:BulkProgressEvent):void
		{
			if(moduleProgFunc != null)
				moduleProgFunc(evt);
		}
		private var configCompFunc:Function = null;
		private var configProgFunc:Function = null;
		private var moduleCompFunc:Function = null;
		private var moduleProgFunc:Function = null;
	}
}